package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectTask;
import com.mxpio.erp.common.plm.entity.ProjectTaskDeliverableTemplate;
import com.mxpio.erp.common.plm.entity.ProjectTaskTemplate;
import com.mxpio.erp.common.plm.service.ProjectTaskTemplateService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectTaskTemplateController", description = "项目-任务-模板")
@RestController
@RequestMapping("/erp/plm/pro/task/template/")
public class ProjectTaskTemplateController {

    @Autowired
    private ProjectTaskTemplateService projectTaskTemplateService;

    @GetMapping("list")
    @Operation(summary = "任务模板列表", description = "任务模板列表（无分页）", method = "GET")
    public Result<List<ProjectTaskTemplate>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectTaskTemplate> list = projectTaskTemplateService.list(ProjectTaskTemplate.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "任务模板列表", description = "任务模板列表（分页）", method = "GET")
    public Result<Page<ProjectTaskTemplate>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectTaskTemplate> pageResult = projectTaskTemplateService.listPage(ProjectTaskTemplate.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{taskCode}")
    @Operation(summary = "根据任务编码-获取任务信息", description = "根据任务编码-获取任务信息", method = "GET")
    public Result<ProjectTaskTemplate> getById(@PathVariable(name = "taskCode", required = true) String taskCode) {
        ProjectTaskTemplate taskTemplate = projectTaskTemplateService.getById(ProjectTaskTemplate.class, taskCode);
        return Result.OK(taskTemplate);
    }

    @RequestMapping("add")
    @Operation(summary = "保存任务模板", description = "保存任务模板")
    public Result<?> add(@RequestBody ProjectTaskTemplate taskTemplate) {
        return projectTaskTemplateService.saveAll(taskTemplate);
//        projectTaskTemplateService.save(taskTemplate,new SmartCrudPolicyAdapter(){
//            @Override
//            public boolean beforeInsert(CrudContext context) {
//                Object o = context.getEntity();
//                if (o instanceof ProjectTaskDeliverableTemplate){
//                    ProjectTaskTemplate taskTemplate = context.getParent();
//                    ((ProjectTaskDeliverableTemplate) o).setTaskCode(taskTemplate.getTaskCode());
//                    ((ProjectTaskDeliverableTemplate) o).setTemplateCode(taskTemplate.getTemplateCode());
//                }
//                return true;
//            }
//        });
//        return Result.OK(taskTemplate);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑任务模板", description = "编辑任务模板", method = "PUT")
    public Result<?> edit(@RequestBody ProjectTaskTemplate taskTemplate) {
        return projectTaskTemplateService.updateAll(taskTemplate);
//        projectTaskTemplateService.update(taskTemplate,new SmartCrudPolicyAdapter(){
//            @Override
//            public boolean beforeInsert(CrudContext context) {
//                Object o = context.getEntity();
//                if (o instanceof ProjectTaskDeliverableTemplate){
//                    ProjectTaskTemplate taskTemplate = context.getParent();
//                    ((ProjectTaskDeliverableTemplate) o).setTaskCode(taskTemplate.getTaskCode());
//                    ((ProjectTaskDeliverableTemplate) o).setTemplateCode(taskTemplate.getTemplateCode());
//                }
//                return true;
//            }
//        });
//
//        return Result.OK(taskTemplate);
    }


    @DeleteMapping("delete/{taskCode}")
    @Operation(summary = "通过taskCode删除", description = "通过taskCode删除", method = "DELETE")
    public Result<?> delete(@PathVariable(name="taskCode",required=true) String taskCode) {
        return projectTaskTemplateService.deleteAll(taskCode);
    }
//
//    @GetMapping("list/task/tree/{templateCode}")
//    @Operation(summary = "根据项目模板编码-获取任务模板", description = "根据项目模板编码-获取任务模板", method = "GET")
//    public Result<List<ProjectTaskTemplate>> getByList(@PathVariable(name = "templateCode", required = true) String templateCode) {
//        Criteria criteria = Criteria.create().addCriterion("templateCode", Operator.EQ, templateCode);
//        List<ProjectTaskTemplate> list = projectTaskTemplateService.tree(ProjectTaskTemplate.class, criteria);
//        return Result.OK(list);
//    }

    @GetMapping("list/task/tree/{templateCode}")
    @Operation(summary = "获取任务树", description = "获取任务树", method = "GET")
    public Result<List<ProjectTaskTemplate>> listTree(@PathVariable(name = "templateCode", required = true) String templateCode, @Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        criteria = criteria.addCriterion("templateCode", Operator.EQ, templateCode);
        List<ProjectTaskTemplate> tree = projectTaskTemplateService.tree(ProjectTaskTemplate.class, criteria);
        return Result.OK(tree);
    }
}
